package com.zhouyu.fileSync.FScommon.api;

import com.alibaba.fastjson.JSONObject;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;

import java.nio.charset.Charset;

/**
 * Created by zhouyu@lightinthebox.com on 2019/8/14
 **/
public class JSONDecoder extends ChannelInboundHandlerAdapter {

    public JSONDecoder(){
    }

    public void channelRead(ChannelHandlerContext ctx, Object in) {
        if (in instanceof  ByteBuf) {
            ByteBuf decode = (ByteBuf) in;
            int data_len = decode.readableBytes();
            byte[] bytes = new byte[data_len];
            decode.readBytes(bytes);
            Object parse = JSONObject.parse(bytes);
            ctx.fireChannelRead(parse);
        }
    }


}
